﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace TestFramework
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("table1");

            DataTable table = ds.Tables["table1"];
            table.Columns.Add("id", typeof(int));
            table.Columns.Add("name", typeof(string));

            for (int i = 0; i < 10; i++)
            {
                DataRow dr = table.NewRow();
                dr["id"] = i;
                dr["name"] = string.Format("Customer{0}", i);
                table.Rows.Add(dr);
            }
            // 


            for (int i = 10; i < 20; i++)
            {
                DataRow dr = table.NewRow();
                dr["id"] = i;
                dr["name"] = string.Format("Customer{0}", i);
                table.Rows.Add(dr);
            }

            string sort = "Name desc";
            string filter = "id>5";
            table.AcceptChanges();
            DataRow[] matches = table.Select("id=17");

            DataTable tablecone = table.Clone();

            table.PrimaryKey = new DataColumn[] { table.Columns[0] };

            DataRow dr1 = table.Rows.Find(1);

            foreach(DataRow dr in matches)
            {
                dr["name"] = "AAA";
            }

            DataRow[] drs = tablecone.Select(filter, sort);

            PrintDataRows(drs);


            //drs = table.Select();

            //PrintDataRows(drs);

            Console.ReadLine();
        }

        private static void DemonstrateDataView()
        {
            // Create a DataTable with three columns.
            DataTable table = new DataTable("NewTable");
            Console.WriteLine("Original table name: " + table.TableName);
            DataColumn column = new DataColumn("ID", typeof(System.Int32));
            table.Columns.Add(column);

            column = new DataColumn("Category", typeof(System.String));
            table.Columns.Add(column);

            column = new DataColumn("Product", typeof(System.String));
            table.Columns.Add(column);

            column = new DataColumn("QuantityInStock", typeof(System.Int32));
            table.Columns.Add(column);


            // Add some items.
            DataRow row = table.NewRow();
            row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
            table.Rows.Add(row);

            row = table.NewRow();
            row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
            table.Rows.Add(row);

            row = table.NewRow();
            row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
            table.Rows.Add(row);

            row = table.NewRow();
            row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
            table.Rows.Add(row);

            // Mark all rows as "accepted". Not really required
            // for this particular example.
            table.AcceptChanges();

            // Print current table values.
            PrintTableOrView(table, "Current Values in Table");

            DataView view = new DataView(table);
            view.RowFilter = "QuantityInStock > 15";
            PrintTableOrView(view, "Current Values in View");

            DataTable newTable = view.ToTable();
            PrintTableOrView(newTable, "Table created from filtered DataView");
            Console.WriteLine("New table name: " + newTable.TableName);

            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }

        private static void PrintDataRows(DataRow[] drs)
        {
            if (drs.Length <= 0)
            {
                Console.WriteLine("no rows found!");
                return;
            }

            foreach (DataRow dr in drs)
            {
                foreach (DataColumn dc in dr.Table.Columns)
                {
                    Console.Write("\t {0}",dr[dc]);
                }
                Console.WriteLine();
            }
        }

        private static void PrintTableOrView(DataView dv, string label)
        {
            System.IO.StringWriter sw;
            string output;
            DataTable table = dv.Table;
            Console.WriteLine(label);

            // Loop through each row in the view. 
            foreach (DataRowView rowView in dv)
            {
                sw = new System.IO.StringWriter();

                // Loop through each column. 
                foreach (DataColumn col in table.Columns)
                {
                    // Output the value of each column's data.
                    sw.Write(rowView[col.ColumnName].ToString() + ", ");
                }

                output = sw.ToString();

                // Trim off the trailing ", ", so the output looks correct.
                if (output.Length > 2)
                    output = output.Substring(0, output.Length - 2);

                // Display the row in the console window.
                Console.WriteLine(output);
            }
            Console.WriteLine();
        }

        private static void PrintTableOrView(DataTable table, string label)
        {
            System.IO.StringWriter sw;
            string output;

            Console.WriteLine(label);

            // Loop through each row in the table. 
            foreach (DataRow row in table.Rows)
            {
                sw = new System.IO.StringWriter();

                // Loop through each column. 
                foreach (DataColumn col in table.Columns)
                {
                    // Output the value of each column's data.
                    sw.Write(row[col].ToString() + ", ");
                }

                output = sw.ToString();

                // Trim off the trailing ", ", so the output looks correct.
                if (output.Length > 2)
                    output = output.Substring(0, output.Length - 2);

                // Display the row in the console window.
                Console.WriteLine(output);
            }
            Console.WriteLine();
        }


        private static void Reference(ref People p1)
        {
            Object p2 = p1;
            Reference1(ref p2);
        }

        private static void Reference1(ref Object p2)
        {
            People p3 = new People();// (People)p;
            People p4 = (People)p2;
            p3.Age = p3.Age + 100;
            p2 = p3;
        }

        private static void Reference(People p)
        {
            p.Age = p.Age + 10;
        }
    }
    class TwoDPoint : System.Object
    {
        public readonly int x, y;

        public TwoDPoint(int x, int y)  //constructor
        {
            this.x = x;
            this.y = y;
        }

        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return false;
            }

            // If parameter cannot be cast to Point return false.
            TwoDPoint p = obj as TwoDPoint;
            if ((System.Object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (x == p.x) && (y == p.y);
        }

        public bool Equals(TwoDPoint p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (x == p.x) && (y == p.y);
        }

        public override int GetHashCode()
        {
            return x ^ y;
        }
    }

    class ThreeDPoint : TwoDPoint
    {
        public readonly int z;

        public ThreeDPoint(int x, int y, int z)
            : base(x, y)
        {
            this.z = z;
        }

        public override bool Equals(System.Object obj)
        {
            // If parameter cannot be cast to ThreeDPoint return false:
            ThreeDPoint p = obj as ThreeDPoint;
            if ((object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return base.Equals(obj) && z == p.z;
        }

        public bool Equals(ThreeDPoint p)
        {
            // Return true if the fields match:
            return base.Equals((TwoDPoint)p) && z == p.z;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() ^ z;
        }
    }

    class People
    {
        public int Age = 10;
    }


}
